jQuery to Preview and Rotate an Image Before Upload using PHP

preview-and-rotate-image-using-jquery-php

Preview image functionality can be easily implemented using jQuery before uploading and rotating image element functionality. You can rotate the image at a particular angle and use PHP to upload the image to the server. In this tutorial, we’ll show you how to preview the image using jQuery and rotate the image before you upload it to the server using PHP.

Before uploading, the rotating image helps the user to correct the orientation of the image when uploading images to the server. The user can preview the image with this feature and correct the orientation before uploading the file. Image rotation is very useful if you want to preview the orientation of the image and rotate the image before uploading it.

The following features will be implemented into the rotate script example image. 

  • Display preview of the selected image using JavaScript.
  • Rotate the image clockwise or anticlockwise angle using jQuery (client-side).
  • Rotate an image using the given angle in degrees using PHP (server-side).
  • Upload the rotated image to the server.

Create an index.html file

Create an HTML form with a file input field (for selecting an image file), hidden input (for defining rotation degrees) and a submit button. Define an HTML element to preview the image.

  • Left button rotates the image anticlockwise.
  • Rigth button rotates the image clockwise.

Read Image Data using JavaScript

In the above code, FilePreview () is a custom JavaScript function that generates an image preview. The FileReader object helps to read the raw file data of the image using JavaScript. Once the image raw content is loaded, the image preview appends in the HTML element using jQuery.

jQuery Library is used to add, remove, and rotate image element, so include the jQuery library first.

jQuery to Selecting Image Preview

jQuery to Rotate an Image

The above code rotates the image on the Left/Right button click event.

  • Based on the selected angle, the degree of rotation is calculated and the transform property set to rotate the image element
  • Set the degree value to the rotation input field for server-side use.

Create upload.php and copy ad paste following code

Rotate and Upload Image to Server using PHP Code

Complete HTML Code:

Resize Image While Uploading with PHP

How to show image thumbnail before upload with jQuery

Dynamically Add Watermark on the Uploaded Image Using PHP

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request OR Chat Using Bottom Right Facebook Chat Box

Related posts